//
//  GQHOwnersFollowedView.m
//  Xanthium
//
//  Created by GuanQinghao on 06/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHOwnersFollowedView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"

@interface GQHOwnersFollowedView () <UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,GQHFollowedCellDelegate>

@end

@implementation GQHOwnersFollowedView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

- (void)loadLocalData {
    
}

- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _qh_userModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"aCell";
    GQHFollowedCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHFollowedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.qh_delegate = self;
    cell.qh_followedButton.tag = indexPath.row;
    
    GQHUserModel *userModel = _qh_userModelArray[indexPath.row];
    
    NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:userModel.qh_avatar];
    NSURL *URL = [NSURL URLWithString:URLString];
    [cell.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
    
    cell.qh_nameLabel.text = userModel.qh_userNickname;
    cell.qh_subtitleLabel.text = [NSString stringWithFormat:@"评分: %.2f分",userModel.qh_score];
    cell.qh_contentLabel.text = userModel.qh_orgName;
    cell.qh_followed = YES;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.barTintColor = kLightGrayColor;
    [headerView addSubview:searchBar];
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    DLog(@"点击键盘上的搜索按钮");
    
}

- (void)qh_followedOrUnfollowed:(UIButton *)sender {
    
    if ([_qh_delegate respondsToSelector:@selector(qh_followedOrUnfollowed:)]) {
        
        [_qh_delegate qh_followedOrUnfollowed:sender];
    }
}

@end
